<template>
	<view class="">
		<u-navbar :is-back="true" :border-bottom="false" title="证书" height="46" title-color="#333" :title-bold="true"
		 :title-size="36">
			<view class="navbar-right" slot="right" @click="Store">
				确认
			</view>
		</u-navbar>
		<view class="textarea_box">
			<u-form :model="form" :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item label-width="150" :label-position="labelPosition" label="证书类别" prop="lpzslb" class="name-box"
				 :label-style="labelStyle">
					<u-input placeholder="请输入证书类别" v-model="form.lpzslb" type="text" :custom-style="{fontSize: '30rpx',}" @click="show= true" :height="30" :clearable="false"></u-input>
					<u-picker v-model="show" mode="selector" :range="lplbbmData" range-key="name" @confirm="confirmlpzslb"></u-picker>
				</u-form-item>
				
				<u-form-item label-width="150" :label-position="labelPosition" label="证书编号" prop="lpzsbh" class="name-box"
				 :label-style="labelStyle">
					<u-input placeholder="请输入证书编号" v-model="form.lpzsbh" type="text" :custom-style="{fontSize: '30rpx',}" :height="30"></u-input>
				</u-form-item>
				
					<u-form-item label-width="150" :label-position="labelPosition" label="证书名称" prop="lpzsmc" class="name-box"
				 :label-style="labelStyle">
					<u-input placeholder="请输入证书名称" v-model="form.lpzsmc" type="text" :custom-style="{fontSize: '30rpx',}" @click="showlpzsmc= true" :height="30" :clearable="false"></u-input>
					<u-picker v-model="showlpzsmc" mode="selector" :range="lpzsbmData" range-key="name" @confirm="confirmlpzsmc"></u-picker>
				</u-form-item>
		
				<u-form-item label-width="150" :label-position="labelPosition" label="注册编号" prop="lpzszcbh" class="name-box"
				 :label-style="labelStyle">
					<u-input placeholder="请输入注册编号" v-model="form.lpzszcbh" type="text" :custom-style="{fontSize: '30rpx',}" :height="30"></u-input>
				</u-form-item>
			</u-form>
			<view class="" style="text-align: center;margin-top: 30rpx;padding: 0 100rpx;" v-if="mkscdtn">
				<u-button type="success"  shape="circle" @click="mkdelete">删除</u-button>
			</view>
			<u-toast ref="uToast"/>
		</view>

	</view>

</template>

<script>
	export default {
		data() {
			return {
				mkscdtn:false,
				show: false,
				showlpzsmc:false,
				id: "",
				index: "",
				mode: "time",
				form: {
					lpzslb: "八大员",
					lplbbm: "2",
					lpzsmc: "施工员",
					lpzsbm: "1045",
					lpzsbh: "",
					lpzszcbh: ""
				},
				lplbbmData:[{
					id:2,
					name:"八大员"
				},
				{
					id:3,
					name:"执业资格"
				},
				{
					id:4,
					name:"职称"
				},
				],
				lpzsbmData:[],
				gzjlData: [],
				rules: {},
				errorType: ['message'],
				labelPosition: "top",
				labelStyle: {
					height: "36rpx",
					fontSize: "24rpx",
					color: "#666666",
				},
			}
		},
		async onLoad(options) {
			this.id = options.id
			if (options.index) {
				this.index = options.index
			}
			await this.resumemy(options.id)
			await this.lCodedata(2)
		},
		methods: {
			confirmlpzslb(e){
				this.form.lpzslb=this.lplbbmData[e].name
				this.form.lplbbm=this.lplbbmData[e].id
				this.lCodedata(this.form.lplbbm)
				this.form.lpzsmc=""
				this.form.lpzsbm=""
			},
			confirmlpzsmc(e){
				this.form.lpzsmc=this.lpzsbmData[e].name
				this.form.lpzsbm=this.lpzsbmData[e].id
			},
			async mkdelete(){
				this.gzjlData.splice(this.index,1)
				this.gzjlData = JSON.stringify(this.gzjlData)
				let $t=this
				let params = {
					ak: this.$store.state.token,
					cmd: 'lPeople_myupdate',
					para: {
				    "zs":this.gzjlData,
						"id": this.id
					}
				}
				const res = await this.$u.api.resumexgdata(params)
				if(res.ResponseID==0){
					this.$refs.uToast.show({
						title: res.Message,
						icon:false,
						duration:800,
						callback(){
						uni.$emit('refreshData');
						uni.navigateBack({
							delta: 1
						})
						}
					})
					
				}
			},
			async resumemy(id) {
				let params = {
					ak: this.$store.state.token,
					cmd: 'lPeople_myquery',
					para: {
						"id": id,
					}
				}
				const res = await this.$u.api.resumedata(params)
				if(res.Data.zs){
					this.gzjlData = JSON.parse(res.Data.zs)
				}
				if (this.index) {
					this.mkscdtn=true
					this.form.lpzslb = this.gzjlData[this.index].lpzslb
					this.form.lplbbm = this.gzjlData[this.index].lplbbm
					this.form.lpzsmc = this.gzjlData[this.index].lpzsmc
					this.form.lpzsbm = this.gzjlData[this.index].lpzsbm
					this.form.lpzsbh = this.gzjlData[this.index].lpzsbh
					this.form.lpzszcbh = this.gzjlData[this.index].lpzszcbh
				} else {
					this.index = this.gzjlData.length
					this.gzjlData[this.index] = { ...this.form
					}
				}
			},
			async lCodedata(id) {
				let params = {
					cmd: 'lCode_list_proc',
					para: {
						"pageindex": 1,
						"pagesize": 1000
					}
				}
				const res = await this.$u.api.lCodedata(params)
				this.lpzsbmData = res.Data.Data.filter(item => item.ctype == id);
			},
			
			async Store() {
				let $t=this
				this.gzjlData = JSON.stringify(this.gzjlData)
				let params = {
					ak: this.$store.state.token,
					cmd: 'lPeople_myupdate',
					para: {
						"zs": this.gzjlData,
						"id": this.id
					}
				}
				const res = await this.$u.api.resumexgdata(params)
				if(res.ResponseID==0){
					this.$refs.uToast.show({
						title: res.Message,
						icon:false,
						duration:800,
						callback(){
							uni.$emit('refreshData');
							uni.navigateBack({
								delta: 1
							})
						}
					})
					
				}
			}
		},
		watch: {
			form: {
				handler(newName, oldName) {
					this.gzjlData[this.index] = { ...this.form
					}
				},
				immediate: false,
				deep: true,
			}

		},
	}
</script>

<style scoped lang="scss">
	.name-box {
		padding: 30rpx;
	}

	.navbar-right {
		margin-right: 30rpx;
		font-size: 30rpx;
	}
</style>
